package cn.message.service.impl;

import cn.itcast.mapper.classsifyMapper;
import cn.itcast.pojo.classsify;
import cn.itcast.pojo.classsifyExample;
import cn.message.service.IClassifyService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 郭智超
 * @create 2020-03-28 9:23
 */
@Service()
public class ClassifyServiceImpl implements IClassifyService {

    @Resource
    private classsifyMapper classsifymapper;

    @Override
    public Map<String, Object> getAllClassify(Integer page, Integer limit, classsify classsify) {
        System.out.println("开始执行getAllClassify方法了");
        classsifyExample cfe = new classsifyExample();
        classsifyExample.Criteria criteria = cfe.createCriteria();

        //添加查询条件
        //根据编号模糊查询
        if(!classsify.getNumber().isEmpty()){
            criteria.andNumberLike("%"+classsify.getNumber()+"%");
        }
        //根据商品名称模糊查询
        if(!classsify.getName().isEmpty()){
            criteria.andNameLike("%"+classsify.getName()+"%");
        }
        //根据fid模糊查询
        if(classsify.getFid() != null){
            criteria.andFidEqualTo(classsify.getFid());
        }
        //返回符合查询条件的总记录数
        int count = classsifymapper.countByExample(cfe);
        //分页
        PageHelper.startPage(page, limit);
        //查询所有符合条件的商品分类的信息
        List<classsify> classsifyList = classsifymapper.selectByExample(cfe);
        //格式化日期
        /*SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (classsify classsify : classsifyList) {
            if(classsify.getRegistrationTime() != null){
                classsify.setRegistrationTime_string(sdf.format(classsify.getRegistrationTime()));
            }
        }*/
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("count",count);
        map.put("classsifyList",classsifyList);

        return map;
    }

    @Override
    public Boolean deleteClassifyById(int id) {
        int i = classsifymapper.deleteByPrimaryKey(id);
        if(i != 1){
            return false;
        }
        return true;
    }

    @Override
    public Boolean addClassify(classsify classsify) {
        int i = classsifymapper.insertSelective(classsify);
        if(i != 1){
            return false;
        }
        return true;
    }

    @Override
    public List<classsify> getAllMainClassifyName() {
        classsifyExample cfe = new classsifyExample();
        classsifyExample.Criteria criteria = cfe.createCriteria();
        criteria.andFidEqualTo(0);
        List<classsify> classsifyList = classsifymapper.selectByExample(cfe);
        return classsifyList;
    }

    @Override
    public Boolean updateClassify(classsify classsify) {
        int i = classsifymapper.updateByPrimaryKeySelective(classsify);
        if(i != 1){
            return false;
        }
        return true;
    }

}
